package com.hitqz.robot.biz.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.hitqz.robot.api.business.dto.CmdRobot2PosDto;
import com.hitqz.robot.api.business.dto.RobotControlButtonDto;
import com.hitqz.robot.biz.service.RobotClientCmdService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * 执行端控制
 *
 * @author wengyongxin
 * @date 2024-05-28 08:21:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/client/cmd" )
@Tag(description = "client" , name = "执行端控制" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Slf4j
public class RobotClientCmdController {

	private final RobotClientCmdService robotClientCmdService;

	/**
	 * 获取所有机器人设备点位
	 * @param clientId
	 * @return
	 */
	@Operation(summary = "执行端控制" , description = "下发命令到执行端" )
    @PostMapping("/{clientId}" )
	@SysLog("给机器人下发控制指令" )
    public R<Boolean> send(@PathVariable(value = "clientId") String clientId,
						   @RequestBody RobotControlButtonDto button){
		log.info("to clientId:{},data: {}",clientId,button);
		return R.ok(robotClientCmdService.send(clientId,button));
    }


	@Operation(summary = "执行端控制", description = "前往某个站点")
	@PostMapping("/goStation/{clientId}/{posCode}")
	public R goStation(@PathVariable(value = "clientId") String clientId,
					   @PathVariable(value = "posCode") String posCode) {
		CmdRobot2PosDto dto = new CmdRobot2PosDto();
		dto.setClientId(clientId);
		dto.setPosCode(posCode);
		SpringUtil.getApplicationContext().publishEvent(dto);
		return R.ok();
	}




}